﻿using MetaProperties.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MetaProperties.Framework;
using System;
using System.Collections.Generic;

namespace MetaProperties.Core.Tests
{
    using System.Linq;

    using Rhino.Mocks;


    /// <summary>
    ///This is a test class for MetaPropertyHostExtensionsTest and is intended
    ///to contain all MetaPropertyHostExtensionsTest Unit Tests
    ///</summary>
    [TestClass()]
    public class MetaPropertyHostExtensionsTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Initialize
        ///</summary>
        [TestMethod()]
        public void InitializeTest5()
        {
            MockRepository mocks = new MockRepository();

            IMetaPropertyHost host = mocks.DynamicMock<IMetaPropertyHost>();
            IMetaProperty metaProperty = mocks.DynamicMock<IMetaProperty>();

            Attribute[] attributes = new Attribute[] { new SetEventFilterAttribute() };

            Expect.Call(() => metaProperty.Initialize(null)).IgnoreArguments()
                .Do((Action<MetaPropertyConfiguration>)delegate(MetaPropertyConfiguration c)
                {
                    Assert.AreEqual(host, c.Host);
                    Guid g = new Guid(c.PropertyName);
                    Assert.AreEqual(attributes.Length, c.Attributes.Count());
                    Assert.AreEqual(attributes[0], c.Attributes.First());
                })
                .Repeat.Once();

            mocks.ReplayAll();

            Extensions.Initialize(host, metaProperty, attributes);

            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for Initialize
        ///</summary>
        [TestMethod()]
        public void InitializeTest4()
        {
            MockRepository mocks = new MockRepository();

            IMetaPropertyHost host = mocks.DynamicMock<IMetaPropertyHost>();
            IMetaProperty metaProperty = mocks.DynamicMock<IMetaProperty>();

            string name = "TestName";

            Expect.Call(() => metaProperty.Initialize(null)).IgnoreArguments()
                .Do((Action<MetaPropertyConfiguration>)delegate(MetaPropertyConfiguration c)
                {
                    Assert.AreEqual(host, c.Host);
                    Assert.AreEqual(name, c.PropertyName);
                    Assert.AreEqual(0, c.Attributes.Count());
                })
                .Repeat.Once();

            mocks.ReplayAll();

            Extensions.Initialize(host, metaProperty, name);

            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for Initialize
        ///</summary>
        [TestMethod()]
        public void InitializeTest3()
        {
            MockRepository mocks = new MockRepository();

            IMetaPropertyHost host = mocks.DynamicMock<IMetaPropertyHost>();
            IMetaProperty metaProperty = mocks.DynamicMock<IMetaProperty>();

            Expect.Call(() => metaProperty.Initialize(null)).IgnoreArguments()
                .Do((Action<MetaPropertyConfiguration>)delegate(MetaPropertyConfiguration c)
                {
                    Assert.AreEqual(host, c.Host);
                    Guid g = new Guid(c.PropertyName);
                    Assert.AreEqual(0, c.Attributes.Count());
                })
                .Repeat.Once();

            mocks.ReplayAll();

            Extensions.Initialize(host, metaProperty);

            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for Initialize
        ///</summary>
        [TestMethod()]
        public void InitializeTest2()
        {
            MockRepository mocks = new MockRepository();

            IMetaPropertyHost host = mocks.DynamicMock<IMetaPropertyHost>();
            IMetaProperty metaProperty = mocks.DynamicMock<IMetaProperty>();

            string name = "TestName";
            IEnumerable<Attribute> attributes = new List<Attribute> { new SetEventFilterAttribute() };

            Expect.Call(() => metaProperty.Initialize(null)).IgnoreArguments()
                .Do((Action<MetaPropertyConfiguration>)delegate(MetaPropertyConfiguration c)
                {
                    Assert.AreEqual(host, c.Host);
                    Assert.AreEqual(name, c.PropertyName);
                    Assert.AreEqual(attributes.Count(), c.Attributes.Count());
                    Assert.AreEqual(attributes.First(), c.Attributes.First());
                })
                .Repeat.Once();

            mocks.ReplayAll();

            Extensions.Initialize(host, metaProperty, name, attributes);

            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for Initialize
        ///</summary>
        [TestMethod()]
        public void InitializeTest1()
        {
            MockRepository mocks = new MockRepository();

            IMetaPropertyHost host = mocks.DynamicMock<IMetaPropertyHost>();
            IMetaProperty metaProperty = mocks.DynamicMock<IMetaProperty>();

            string name = "TestName";
            Attribute[] attributes = new Attribute[] { new SetEventFilterAttribute() };

            Expect.Call(() => metaProperty.Initialize(null)).IgnoreArguments()
                .Do((Action<MetaPropertyConfiguration>)delegate(MetaPropertyConfiguration c)
                {
                    Assert.AreEqual(host, c.Host);
                    Assert.AreEqual(name, c.PropertyName);
                    Assert.AreEqual(attributes.Length, c.Attributes.Count());
                    Assert.AreEqual(attributes[0], c.Attributes.First());
                })
                .Repeat.Once();

            mocks.ReplayAll();

            Extensions.Initialize(host, metaProperty, name, attributes);

            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for Initialize
        ///</summary>
        [TestMethod()]
        public void InitializeTest()
        {
            MockRepository mocks = new MockRepository();

            IMetaPropertyHost host = mocks.DynamicMock<IMetaPropertyHost>();
            IMetaProperty metaProperty = mocks.DynamicMock<IMetaProperty>();

            IEnumerable<Attribute> attributes = new List<Attribute> { new SetEventFilterAttribute() };

            Expect.Call(() => metaProperty.Initialize(null)).IgnoreArguments()
                .Do((Action<MetaPropertyConfiguration>)delegate(MetaPropertyConfiguration c)
                {
                    Assert.AreEqual(host, c.Host);
                    Guid g = new Guid(c.PropertyName);
                    Assert.AreEqual(attributes.Count(), c.Attributes.Count());
                    Assert.AreEqual(attributes.First(), c.Attributes.First());
                })
                .Repeat.Once();

            mocks.ReplayAll();

            Extensions.Initialize(host, metaProperty, attributes);

            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for GetSynchronizationContextContainerFromAttributes
        ///</summary>
        [TestMethod()]
        [DeploymentItem("MetaProperties.Core.dll")]
        public void GetSynchronizationContextContainerFromAttributesTest()
        {
            IEnumerable<Attribute> attributes = new List<Attribute>
            {
                new SetEventFilterAttribute(typeof(int)),
                new SetSynchronizationContextContainerAttribute(typeof(SynchronizationContextContainer))
            };

            ISynchronizationContextContainer actual
                = Extensions_Accessor
                .GetSynchronizationContextContainerFromAttributes(attributes);

            Assert.IsNotNull(actual);
            Assert.AreEqual(typeof(SynchronizationContextContainer), actual.GetType());
        }

        /// <summary>
        ///A test for GetSynchronizationContextContainer
        ///</summary>
        [TestMethod()]
        public void GetSynchronizationContextContainerTest()
        {
            IEnumerable<Attribute> attributes = new List<Attribute>
            {
                new SetEventFilterAttribute(typeof(int)),
                new SetSynchronizationContextContainerAttribute(typeof(SynchronizationContextContainer))
            };

            MetaPropertyConfiguration configuration = new MetaPropertyConfiguration(
                this,
                "TestName",
                attributes);

            ISynchronizationContextContainer actual
                = Extensions.GetSynchronizationContextContainer(configuration);

            Assert.IsNotNull(actual);
            Assert.AreEqual(typeof(SynchronizationContextContainer), actual.GetType());
        }
    }
}
